package com.hemi.rhet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import org.androidpn.client.ServiceManager;
import org.apache.http.client.ClientProtocolException;

import android.app.ListActivity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

import com.hemi.rhet.PullToRefreshListView.OnRefreshListener;
import com.hemi.rhet.html.TopicList;
import com.hemi.rhet.util.ApplicationContext;
import com.hemi.rhet.util.HttpUtil;

public class PullToRefreshActivity extends ListActivity {    
    private LinkedList<String> mListItems;
    WebView mWebView = null;
    private Handler mHandler = new Handler();
    /** Called when the activity is first created. */
    
    ServiceManager serviceManager =null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pull_to_refresh);
        ((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                // Do work to refresh the list here.
                new GetDataTask().execute();
            }
        });

 
	     

        LayoutInflater mLayoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        List<View> data =new ArrayList<View>();
        View timelist = mLayoutInflater.inflate(R.layout.topic_list_itme, null);
//        View timelist2 = mLayoutInflater.inflate(R.layout.pull_to_refresh_header, null);
//        data.add(timelist2);
        data.add(timelist);
        
	    
        
         mWebView =(WebView) timelist.findViewById(R.id.postdispalyview);
		WebSettings webSettings = mWebView.getSettings();
		webSettings.setDefaultTextEncodingName("utf-8");
		webSettings.setSavePassword(false);
		webSettings.setSaveFormData(false);
		webSettings.setJavaScriptEnabled(true);
		webSettings.setSupportZoom(false);
		webSettings.setBuiltInZoomControls(false);
		mWebView.setInitialScale(100);// 初始显示比例100%
		mWebView.setMinimumHeight(100);
		mWebView.setMinimumWidth(100);
		mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 


		JavaScriptInterfaceBinder.INSTANCE.addCallCaremaAblity(this, mWebView,
				mHandler);
		JavaScriptInterfaceBinder.INSTANCE.addWebDataPostAblity(this, mWebView,
				mHandler);
		JavaScriptInterfaceBinder.INSTANCE.addWebDataUrlRedirectAblity(this,
				mWebView, mHandler);
		JavaScriptInterfaceBinder.INSTANCE.addWebImageSelectAblity(this,
				mWebView, mHandler);
		JavaScriptInterfaceBinder.INSTANCE.addShowTopicDetailAblity(this,
				mWebView, mHandler);
//		mWebView.addJavascriptInterface(new JavaScriptInterface2HiddenProcessBar(), "hiddenProcessBar");   
		
        SelfAdapter adapter= new SelfAdapter(this,data);
        setListAdapter(adapter);
//        new GetDataTask().execute();
        ((PullToRefreshListView) getListView()).prepareForRefresh();
        ((PullToRefreshListView) getListView()).onRefresh();
    }

    private class GetDataTask extends AsyncTask<Void, Void, Object> {

        @Override
        protected Object doInBackground(Void... params) {
        	try {
 				String urlStr = new StringBuffer(ApplicationContext.DOMAIN+"/post/allPostes_4json").toString();
 				HashMap mapparams=  new HashMap();
 				String  content = HttpUtil.postUrl(urlStr, mapparams);
 				String data=TopicList.createNew().compile(PullToRefreshActivity.this,"posteList.template", content);
 				System.out.println("返回："+data);
 				return data;
 			} catch (ClientProtocolException e) {
 				e.printStackTrace();
 				return e;
 			} catch (IOException e) {
 				e.printStackTrace();
 				return e;
 			}finally{
 			}
        }
        protected void onPostExecute(Object result) { 
            if(result!=null &&	(result instanceof String))
            {
            	mWebView.loadDataWithBaseURL("file:///android_asset/.", result.toString(), "text/html", "utf-8", null);  
            }else if(result instanceof Exception){ 
          	  //	Toast.makeText(PullToRefreshActivity.this, "数据加载失败！！",Toast.LENGTH_SHORT).show();            
    		  } 
            ((PullToRefreshListView) getListView()).onRefreshComplete();
        }
    }

    protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
//		if(serviceManager!=null )
		//serviceManager.stopService();
	}
}
